﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using com.vs.DataAccess.DO;
using com.vs.General;
using com.vs.General.RF;
using com.vs.Task.BL;

namespace com.vs.RF.Web.task
{
    public partial class PutToEntryHandling : RFHandlingPage
    {
        private string stLoc = null;
        private string stLPN = null;
        private int iConfirmQty = 0;
        private TaskDetailDO tskDtlDO = null;
        private List<TaskDetailDO> ltToTaskDtl = null;
        private string stAction = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            stSourcePage = "task/PutToEntry.aspx";
            stTargetPage = "task/TaskIDEntry.aspx";
            go();
        }

        protected override void initData()
        {
            base.initData();

            ltToTaskDtl = (List<TaskDetailDO>)Session[RFSupport.TO_TASK_DETAIL_DO_LIST];
            if (ltToTaskDtl == null || ltToTaskDtl.Count < 1)
            {
                stError = MessageManager.MSG_TSK_012; //没有可以放货的任务
                return;
            }
            tskDtlDO = ltToTaskDtl[0];
        }

        protected override void parseRequest()
        {
            stLoc = Request["tbLoc"];
            stLPN = Request["tbLPN"];
            stAction = Request["hdAction"];
            iConfirmQty = Convert.ToInt32(Request["tbQty"]);
        }

        protected override void procData()
        {
            if (StringManager.Equals(stAction, RFSupport.PUTAWAY))
                this.procConfirm();
            else if (StringManager.Equals(stAction, RFSupport.SKIP))
                this.procSkip();
        }

        private void procConfirm()
        {

            TaskExecutionHelper tskMgr = null;
            TaskConfirmDS tcDS = null;

            try
            {
                tskMgr = new TaskExecutionHelper();
                tcDS = new TaskConfirmDS();
                tcDS.ACTUAL_FROM_LOC = tskDtlDO.CURRENT_LOC;
                tcDS.ACTUAL_FROM_LPN = tskDtlDO.CURRENT_LPN;
                tcDS.ACTUAL_TO_LOC = tskDtlDO.TO_LOC;
                tcDS.ACTUAL_TO_LPN = tskDtlDO.TO_LPN;
                tcDS.CONFIRM_USER = rfSess.User;
                tcDS.CONFIRM_QTY = this.iConfirmQty;
                tcDS.INTERNAL_TASK_LINE_NUM = tskDtlDO.INTERNAL_TASK_LINE_NUM;
                tcDS.QUANTITY_UM = tskDtlDO.QUANTITY_UM;

                stError = tskMgr.ConfirmToTask(rfSess, tcDS);

                if (!StringManager.IsEmpty(stError))
                    return;
                else
                {
                    ltToTaskDtl.RemoveAt(0);
                }

                //到达最后一条任务
                if (this.ltToTaskDtl.Count < 1)
                    stTargetPage = "task/TaskIDEntry.aspx";
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(rfSess, e);
                stError = e.Message;
            }
        }

        private void procSkip()
        {
            
            try
            {
                if (ltToTaskDtl.Count > 1)
                {
                    ltToTaskDtl.RemoveAt(0);
                    ltToTaskDtl.Add(tskDtlDO);
                    Session[RFSupport.TO_TASK_DETAIL_DO_LIST] = ltToTaskDtl;
                }
                else
                    stError = MessageManager.MSG_TSK_024;//没有其余任务明细了。
            }
            catch (Exception e)
            {
                ExceptionManager.LogException(rfSess, e);
                stError = e.Message;
            }
        }
    }
}
